﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Accounting.Library;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhomHangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_LoaiHangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using Converter;
using System.Resources;
using System.Reflection;

namespace csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa
{
    public partial class DM_HangHoa_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "HangHoa"; }
        }

        #endregion

        #region Load Methods
        public DM_HangHoa_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_HangHoa_GeneralInformation_GroupBox, controls);
            this.DM_HangHoa_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource dm_HangHoa_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                dm_HangHoa_collectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_Kho_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_collectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHangHoa_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHangHoa_ListViewModelSource"];
                DM_LoaiHangHoa_ListViewModel dm_LoaiHangHoa_ListViewModel = new DM_LoaiHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHangHoa_collectionViewSource.Source = new List<object> { dm_LoaiHangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_NhomHangHoa_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhomHangHoa_ListViewModelSource"];
                DM_NhomHangHoa_ListViewModel dm_NhomHangHoa_ListViewModel = new DM_NhomHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_NhomHangHoa_collectionViewSource.Source = new List<object> { dm_NhomHangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_collectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_collectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

              

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_collectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                dm_NhaCungCap_collectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DonViTinh_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_collectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_HangHoa_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ViewModelSource"];
                DM_HangHoa_ViewModel dm_HangHoa_ViewModel = new DM_HangHoa_ViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                dm_HangHoa_ViewSource.Source = new List<object> { dm_HangHoa_ViewModel };
                dm_HangHoa_ViewModel.Saved += new EventHandler(dm_HangHoa_ViewModel_Saved);
              


            }
        }
        #endregion

        #region Switch View Methods
        private void SwitchInputForInformationView(string nameOfView)
        {
            if (nameOfView == "GeneralInfoView")
            {
                this.DM_HangHoa_GeneralInformation_GroupBox.Visibility = Visibility.Visible;
                this.DM_HangHoa_DetailInformation_GroupBox.Visibility = Visibility.Collapsed;
            }
            else if (nameOfView == "DetailInfoView")
            {
                this.DM_HangHoa_DetailInformation_GroupBox.Visibility = Visibility.Visible;
                this.DM_HangHoa_GeneralInformation_GroupBox.Visibility = Visibility.Collapsed;
            }
        }

        private void DetailInfoView_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            SwitchInputForInformationView("DetailInfoView");
        }

        private void GeneralInfoView_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            SwitchInputForInformationView("GeneralInfoView");
        }

        #endregion
        #region  Event Handle

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
        }
        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }

        private void DM_HangHoa_GeneralInformation_NhomHangHoaId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhomHangHoa_ChildWindow dm_NhomHangHoa_ChildWindow = new DM_NhomHangHoa_ChildWindow();
            dm_NhomHangHoa_ChildWindow.Show();
            dm_NhomHangHoa_ChildWindow.SaveClicked += new EventHandler(dm_NhomHangHoa_ViewModel_Saved);
          
        }

        private void DM_HangHoa_GeneralInformation_LoaiHangHoaId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiHangHoa_ChildWindow dm_LoaiHangHoa_ChildWindow = new DM_LoaiHangHoa_ChildWindow();
            dm_LoaiHangHoa_ChildWindow.Show();
            dm_LoaiHangHoa_ChildWindow.SaveClicked += new EventHandler(dm_LoaiHangHoa_ViewModel_Saved);

        }

        private void DM_HangHoa_GeneralInformation_DonViTinhId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_DonViTinh_ChildWindow dm_DonViTinh_ChildWindow = new DM_DonViTinh_ChildWindow();
            dm_DonViTinh_ChildWindow.Show();
            dm_DonViTinh_ChildWindow.SaveClicked += new EventHandler(dm_DonViTinh_ViewModel_Saved);

        }

        private void DM_HangHoa_GeneralInformation_TinhChatHangHoaId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TinhChatHangHoa_ChildWindow dm_TinhChatHangHoa_ChildWindow = new DM_TinhChatHangHoa_ChildWindow();
            dm_TinhChatHangHoa_ChildWindow.Show();
            dm_TinhChatHangHoa_ChildWindow.SaveClicked += new EventHandler(dm_TinhChatHangHoa_ViewModel_Saved);

        }

        private void DM_HangHoa_GeneralInformation_DonViTinhChuyenDoiId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_DonViTinh_ChildWindow dm_DonViTinhChuyenDoi_ChildWindow = new DM_DonViTinh_ChildWindow();
            dm_DonViTinhChuyenDoi_ChildWindow.Show();
            dm_DonViTinhChuyenDoi_ChildWindow.SaveClicked += new EventHandler(dm_DonViTinhChuyenDoi_ViewModel_Saved);

        }

        private void DM_HangHoa_DetailInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);

        }

        private void DM_HangHoa_DetailInformation_ThueSuatId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_ThueSuat_ChildWindow dm_ThueSuat_ChildWindow = new DM_ThueSuat_ChildWindow();
            dm_ThueSuat_ChildWindow.Show();
            dm_ThueSuat_ChildWindow.SaveClicked += new EventHandler(dm_ThueSuat_ViewModel_Saved);

        }

        private void DM_HangHoa_DetailInformation_KhoNgamDinhId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_Kho_ChildWindow dm_KhoNgamDinh_ChildWindow = new DM_Kho_ChildWindow();
            dm_KhoNgamDinh_ChildWindow.Show();
            dm_KhoNgamDinh_ChildWindow.SaveClicked += new EventHandler(dm_KhoNgamDinh_ViewModel_Saved);

        }

        private void DM_HangHoa_DetailInformation_TaiKhoanKhoId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TaiKhoan_ChildWindow dm_TaiKhoanKho_ChildWindow = new DM_TaiKhoan_ChildWindow();
            dm_TaiKhoanKho_ChildWindow.Show();
            dm_TaiKhoanKho_ChildWindow.SaveClicked += new EventHandler(dm_TaiKhoanKho_ViewModel_Saved);

        }

        private void DM_HangHoa_DetailInformation_TaiKhoanChiPhiId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TaiKhoan_ChildWindow dm_TaiKhoanChiPhi_ChildWindow = new DM_TaiKhoan_ChildWindow();
            dm_TaiKhoanChiPhi_ChildWindow.Show();
            dm_TaiKhoanChiPhi_ChildWindow.SaveClicked += new EventHandler(dm_TaiKhoanChiPhi_ViewModel_Saved);

        }

        private void DM_HangHoa_DetailInformation_TaiKhoanDoanhThuId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TaiKhoan_ChildWindow dm_TaiKhoanDoanhThu_ChildWindow = new DM_TaiKhoan_ChildWindow();
            dm_TaiKhoanDoanhThu_ChildWindow.Show();
            dm_TaiKhoanDoanhThu_ChildWindow.SaveClicked += new EventHandler(dm_TaiKhoanDoanhThu_ViewModel_Saved);

        }
        #endregion
        #region View Model Related Methods
        void dm_HangHoa_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_HangHoa_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel vm = ((List<object>)dm_HangHoa_ListViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            vm.GetDM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
        }


        void dm_LoaiHangHoa_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiHangHoa_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHangHoa_ListViewModelSource"];
            DM_LoaiHangHoa_ListViewModel vm = ((List<object>)dm_LoaiHangHoa_ListViewSource.Source)[0] as DM_LoaiHangHoa_ListViewModel;
            vm.GetDM_LoaiHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_TaiKhoanDoanhThu_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanDoanhThu_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel vm = ((List<object>)dm_TaiKhoanDoanhThu_ListViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            vm.GetDM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);

        }

        void dm_TaiKhoanChiPhi_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanChiPhi_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel vm = ((List<object>)dm_TaiKhoanChiPhi_ListViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            vm.GetDM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_TaiKhoanKho_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanKho_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel vm = ((List<object>)dm_TaiKhoanKho_ListViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            vm.GetDM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);

        }

        void dm_NhomHangHoa_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhomHangHoa_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhomHangHoa_ListViewModelSource"];
            DM_NhomHangHoa_ListViewModel vm = ((List<object>)dm_NhomHangHoa_ListViewSource.Source)[0] as DM_NhomHangHoa_ListViewModel;
            vm.GetDM_NhomHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_ThueSuat_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_ThueSuat_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel vm = ((List<object>)dm_ThueSuat_ListViewSource.Source)[0] as DM_ThueSuat_ListViewModel;
            vm.GetDM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
        }

        void dm_KhoNgamDinh_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_KhoNgamDinh_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
            DM_Kho_ListViewModel vm = ((List<object>)dm_KhoNgamDinh_ListViewSource.Source)[0] as DM_Kho_ListViewModel;
            vm.GetDM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_DonViTinh_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_DonViTinh_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
            DM_DonViTinh_ListViewModel vm = ((List<object>)dm_DonViTinh_ListViewSource.Source)[0] as DM_DonViTinh_ListViewModel;
            vm.GetDM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);

        }

        void dm_TinhChatHangHoa_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
            DM_TinhChatHangHoa_ListViewModel vm = ((List<object>)dm_TinhChatHangHoa_ListViewSource.Source)[0] as DM_TinhChatHangHoa_ListViewModel;
            vm.GetDM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_DonViTinhChuyenDoi_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_DonViTinhChuyenDoi_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
            DM_DonViTinh_ListViewModel vm = ((List<object>)dm_DonViTinhChuyenDoi_ListViewSource.Source)[0] as DM_DonViTinh_ListViewModel;
            vm.GetDM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);

           
        }

        private void OnCellDoubleClick(object sender, RadRoutedEventArgs args)
        {
            if (this.DM_HangHoa_GridView.SelectedItem != null)
            {
                System.Windows.Data.CollectionViewSource dm_HangHoa_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ViewModelSource"];
                DM_HangHoa_ViewModel vm = ((List<object>)dm_HangHoa_ViewSource.Source)[0] as DM_HangHoa_ViewModel;
                int parameter = ((DM_HangHoaInfo)this.DM_HangHoa_GridView.SelectedItem).Khoa;
                Csla.Xaml.ExecuteEventArgs eventObject = new Csla.Xaml.ExecuteEventArgs();
                eventObject.MethodParameter = parameter;
                vm.GetDM_HangHoa(this, eventObject);
            }
        }
        #endregion

    }
}
